package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HJ35_查找树中元素 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/35.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int N = in.nextInt();
        List<List<Integer>> nodes = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            nodes.add(new ArrayList<>());
        }
        int[] level = new int[N];
        int[] date = new int[N];
        in.nextLine(); // 吸收回车
        nodes.get(0).add(0);
        for (int i = 0; i < N; i++) {
            String[] sArray = in.nextLine().split(" ");
            int len = sArray.length;
            int[] nums = new int[len];
            for (int j = 0; j < len; j++) {
                nums[j] = Integer.parseInt(sArray[j]);
            }
            date[i] = nums[0];
            int lev = level[i] + 1;
            for (int j = 1; j < len; j++) {
                level[nums[j]] = lev;
                nodes.get(lev).add(nums[j]);
            }
        }
        int queryX = in.nextInt();
        int queryY = in.nextInt();
        if (queryX > N - 1 || queryY > nodes.get(queryX).size() - 1) {
            System.out.println("{}");
            return;
        }
        System.out.println("{" + date[nodes.get(queryX).get(queryY)] + "}");
    }
}
